﻿#include "MeasureView/ToolBarSemiAutoMeasure.h"
#include "MeasureView/MeasureView.h"
#include <QAction>
#include <QActionGroup>
#include <QIcon>
#include <QMenu>
namespace Qly
{
ToolBarSemiAutoMeasure::ToolBarSemiAutoMeasure(const QString &title, QWidget *parent)
:QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    pMeasure = nullptr;
}

ToolBarSemiAutoMeasure::ToolBarSemiAutoMeasure(QWidget *parent)
:QToolBar(parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    pMeasure = nullptr;
}

void ToolBarSemiAutoMeasure::attach(MeasureView *widget)
{
    pMeasure = widget;
}

void ToolBarSemiAutoMeasure::attach(QMenu *pMenu)
{
    pMenu->addAction(actionParallel);
    pMenu->addAction(actionInnerCircle);
    pMenu->addAction(actionOuterCircle);
    pMenu->addSeparator();
    pMenu->addAction(actionSetup);

}

void ToolBarSemiAutoMeasure::uncheckAll()
{
    actionParallel->setChecked(false);
    actionOuterCircle->setChecked(false);
}

void ToolBarSemiAutoMeasure::initAction()
{
    setWindowTitle("SemiAuto Measurement Toolbar");
    setObjectName(QStringLiteral("ToolBarSemiAutoMeasure"));

    actionParallel = new QAction(tr("Parallel"), this);
    actionParallel->setObjectName(QStringLiteral("actionParallel"));
    actionParallel->setCheckable(true);
    QIcon iconSelect;
    iconSelect.addFile(QStringLiteral(":/Measurement/autopline.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionParallel->setIcon(iconSelect);


    actionOuterCircle = new QAction(tr("OuterCircle"), this);
    actionOuterCircle->setObjectName(QStringLiteral("actionOuterCircle"));
    actionOuterCircle->setCheckable(true);
    QIcon iconOuterCircle;
    iconOuterCircle.addFile(QStringLiteral(":/Measurement/OuterCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionOuterCircle->setIcon(iconOuterCircle);

    actionInnerCircle = new QAction(tr("InnerCircle"), this);
    actionInnerCircle->setObjectName(QStringLiteral("actionInnerCircle"));
    actionInnerCircle->setCheckable(true);
    QIcon iconInnerCircle;
    iconInnerCircle.addFile(QStringLiteral(":/Measurement/InnerCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionInnerCircle->setIcon(iconInnerCircle);


    addAction(actionParallel);
    addAction(actionOuterCircle);   
    addAction(actionInnerCircle);

    actionSetup = new QAction(tr("setup"), this);
    actionSetup->setCheckable(false);
    QIcon iconSetup;
    iconSetup.addFile(QStringLiteral(":/Measurement/slider.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionSetup->setIcon(iconSetup);

    actionGroup = new QActionGroup(this);
    actionGroup->addAction(actionParallel);
    actionGroup->addAction(actionOuterCircle);
    actionGroup->addAction(actionInnerCircle);

    addSeparator();
    addAction(actionSetup);


    connect(actionParallel, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionOuterCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionInnerCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));

    connect(actionSetup, SIGNAL(triggered(bool)), this, SIGNAL(showSetupDialog()));
}

void ToolBarSemiAutoMeasure::upateDrawType(bool on)
{
    Q_UNUSED(on);

    if(pMeasure == nullptr) return;
    Qly::MeasureDataSet & data = pMeasure->dataSet();
    QAction * action = (QAction *) sender();
//    uncheckAll();

//    action->setChecked(true);
    if( action == actionParallel )
    {
        data.setElementType(Qly::MeasureElement::LineWidth_SA);
    }

    else if( action == actionOuterCircle )
    {
        data.setElementType(Qly::MeasureElement::CircleCenter_SA);
    }
    else if( action == actionInnerCircle )
    {
        data.setElementType(Qly::MeasureElement::Circle2P_SA);
    }
    emit checked();
}
}
